//
//  CXBtCore.h
//  JwPack
//
//  Created by 陈警卫 on 2025/8/12.
//

#import <Foundation/Foundation.h>

#import "CXBtPeriModel.h"
#import "CXBtConfig.h"

// 蓝牙状态
typedef NS_ENUM(NSUInteger , CXBtState) {
    CXBtStateInit = 0,     // 默认
    CXBtStateUnsupported,  // 不支持
    CXBtStateUnauthorized, // 无授权
    CXBtStatePoweredOff,   // 关闭
    CXBtStatePoweredOn,    // 开启
};

@interface CXBtCore : NSObject

// - 回调

/// 蓝牙状态回调
@property (nonatomic, copy) void(^didState)(CXBtState state);
/// 扫描回调
@property (nonatomic, copy) void(^didScanedPeri)(CXBtPeriModel *periModel);
/// 连接回调
@property (nonatomic, copy) void(^didConncetedPeri)(CXBtPeriModel *periModel);
/// 连接失败回调
@property (nonatomic, copy) void(^didConncetFailPeri)(CXBtPeriModel *periModel);
/// 断开连接回调
@property (nonatomic, copy) void(^didDisconncetPeri)(CXBtPeriModel *periModel);
/// 订阅成功回调
@property (nonatomic, copy) void(^didNotifedPeri)(CXBtPeriModel *periModel);
/// 订阅数据回调
@property (nonatomic, copy) void(^didNotifedValue)(CXBtPeriModel *periModel, NSData *data);
/// 信号回调
@property (nonatomic, copy) void(^didRssiPeri)(CXBtPeriModel *periModel);


// - 数据

/// 蓝牙状态
@property (nonatomic, assign) CXBtState state;

/// 配置
@property (nonatomic, strong) CXBtConfig *config;

/// 外设数组
/// 数组中只有 state:
/// CXBtPeriStateConnecting
/// CXBtPeriStateConnected
/// CXBtPeriStateNotifyed
@property (nonatomic, strong, readonly) NSMutableArray<CXBtPeriModel *> *periModels;

/// 初始化
- (instancetype)initWithConfig:(CXBtConfig *)config;

/// 扫描
- (void)scan;

/// 停止扫描
- (void)stopScan;

/// 连接
- (void)connectPeriModel:(CXBtPeriModel *)periModel;

/// 断开连接
- (void)disconnectPeriModel:(CXBtPeriModel *)periModel;

/// 断开连接
- (void)disconnectAll;

/// 通过外设UUID恢复
- (CBPeripheral *)retrievePeripheralsWithUuid:(NSString *)uuid;

/// 自动连接订阅外设
/// peri 传入:直接连接 不传:则通过scanModel连接
- (void)autoNotifyPeriModel:(CXBtPeriModel *)periModel comple:(void(^)(NSError *error))comple;

/// 添加扫描自动连接外设
/// 添加后重新扫描
- (void)addAutoConnectScanModel:(CXBtScanModel *)scanModel;

/// 是否连接外设(包括订阅)
- (BOOL)isConnectedUuid:(NSString *)uuid;

/// 是否连接外设(包括订阅)
- (BOOL)isConnectedName:(NSString *)name;

/// 是否订阅外设
- (BOOL)isNotifyedUuid:(NSString *)uuid;

/// 是否订阅外设
- (BOOL)isNotifyedName:(NSString *)name;

/// 已订阅的外设数组
- (NSArray<CXBtPeriModel *> *)notifedPeriModels;

/// 开始读取外设信号计时器
- (void)startTimerRssiWithInterval:(NSTimeInterval)interval;

/// 结束读取外设信号计时器
- (void)endTimerRssi;

/// 读取外设信号
- (void)timerRssiAction;


@end

